let THREE=require('three');
import Particle from './particle';


const FRACTION=(num,mul=1)=>{
    let n=num*mul;
    return n-Math.floor(n);
};

export default class ParticleCurve extends Particle{
    constructor(values={}){
        super(values);
        this.props.add({
            tMin:values.tMin||0,
            tMax:values.tMax||1,
            thickness:values.thickness||0.1,
            xScale:values.xScale||2,
            zScale:values.zScale||2,
            tension:values.tension||0.5,
        });
    }
    set controlPoints(vs){
        this.ctlPoints=vs;
        this.curve.points=this.ctlPoints;
    }
    initPropCallback(){
        super.initPropCallback();
        this.props.onMultiKeyChange(()=>{
            this.dt=this.props.tMax.value-this.props.tMin.value;
        },'tMax','tMin');
        this.props.onKeyChange('tension',()=>{
            this.curve.tension=this.props.tension.value;
        });
    }
    initVariable(){
        super.initVariable();
        this.ctlPoints=[new THREE.Vector3(-1,0,0),new THREE.Vector3(1,0,0)];
        this.curve=new THREE.CatmullRomCurve3(this.ctlPoints,true,'catmullrom',this.props.tension.value);
        this.tempVec=new THREE.Vector3();
        this.dt=this.props.tMax.value-this.props.tMin.value;
    }
    setStartVertSpeed(rand,i,t){
        this.curve.getPoint(this.props.tMin.value+FRACTION(rand,1e4)*this.dt,this.tempVec);
        this.verts[i]=this.tempVec.x-this.props.thickness.value+this.props.thickness.value*2*FRACTION(rand,1e5);
        this.verts[i+1]=this.tempVec.y;
        this.verts[i+2]=this.tempVec.z-this.props.thickness.value+this.props.thickness.value*2*FRACTION(rand,1e6);

        this.speeds[i]=this.verts[i]*this.props.xScale.value-this.verts[i];
        this.speeds[i+1]=this.props.speedMin.value+(this.props.speedMax.value-this.props.speedMin.value)*FRACTION(rand,1e7);
        this.speeds[i+2]=this.verts[i+2]*this.props.zScale.value-this.verts[i+2];
    }
}